package com.telrob.antlr.type;

public class FunctionType extends XnType {
	private XnType functionHeader;
	private XnType functionBody;
	
	public FunctionType(XnType functionHeader, XnType functionBody) {
		super();
		this.functionHeader = functionHeader;
		this.functionBody = functionBody;
	}
	public XnType getFunctionHeader() {
		return functionHeader;
	}
	public void setFunctionHeader(XnType functionHeader) {
		this.functionHeader = functionHeader;
	}
	public XnType getFunctionBody() {
		return functionBody;
	}
	public void setFunctionBody(XnType functionBody) {
		this.functionBody = functionBody;
	}
	
	@Override
	public String toString() {
		return "FunctionType [functionHeader=" + functionHeader + ", functionBody=" + functionBody + "]";
	}
	
	
	public void process() {
		FunctionHeaderType header=(FunctionHeaderType)functionHeader;
		FunctionBodyType body=(FunctionBodyType)functionBody;
		
		IdType nameType=(IdType)header.getName();
		String functionName=nameType.getName();
		code.append(functionName+":\r\n");
		code.append("\tpush bp\r\n");
		code.append("\tmovrr bp,sp\r\n");
		
		body.setParent(this);
		body.process();
		//获取临时变量的个数
		int size=body.getSymbolNum()+4;
		code.append("\taddrn sp,"+size+"\r\n");
		String bodyCode=body.getAsmCode();
		if(bodyCode!=null&&!"".equals(bodyCode)) {
			code.append(bodyCode);
		}
		code.append("\tsubrn sp,"+size+"\r\n");
		code.append("\tpop bp\r\n");
		code.append("\tret\r\n");
	}
}
